﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MayfairFramework.Common.Resource
{
    /// <summary>
    /// 
    /// </summary>
    public class Messages
    {
        /// <summary>
        /// 
        /// </summary>
        private IDictionary<string, IList<Message>> messageDictionary = new Dictionary<string, IList<Message>>();

        /// <summary>
        /// 
        /// </summary>
        public Messages()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public bool HasError
        {
            get { return messageDictionary.Count != 0; }
        }

        /// <summary>
        /// 
        /// </summary>
        public ICollection<string> Keys
        {
            get { return messageDictionary.Keys; }
        }

        /// <summary>
        /// 
        /// </summary>
        public int Count
        {
            get
            {
                int ret = 0;
                foreach (string key in this.Keys)
                {
                    IList<Message> list = this.GetMessageList(key);
                    ret = ret + list.Count;
                }
                return ret;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgs"></param>
        public void Add(Messages msgs)
        {
            foreach (string key in msgs.Keys)
            {
                IList<Message> msgList = msgs.GetMessageList(key);
                foreach (Message msg in msgList)
                {
                    Add(key, msg);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="msg"></param>
        public void Add(string key, Message msg)
        {
            IList<Message> list = null;
            if (this.messageDictionary.ContainsKey(key))
            {
                list = this.messageDictionary[key];
            }
            else
            {
                list = new List<Message>();
                this.messageDictionary.Add(key, list);
            }
            list.Add(msg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList<Message> GetMessageList(string key)
        {
            return this.messageDictionary[key];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string[] ToArray()
        {
            IList<string> ret = new List<string>();
            foreach (string key in this.Keys)
            {
                IList<Message> messageList = this.GetMessageList(key);
                foreach (Message msg in messageList)
                {
                    ret.Add(msg.ToString());
                }
            }
            return ret.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string key in this.Keys)
            {
                foreach (Message msg in this.GetMessageList(key))
                {
                    sb.Append(msg);
                    sb.Append(Environment.NewLine);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            return this.messageDictionary.ContainsKey(key);
        }
    }
}
